<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Product extends Model
{
    /**
     * 产品文案
     * @return \think\model\relation\hasOne
     */
    public function details() {
        return $this->hasOne('ProductDetails', '', '', 'LEFT');
    }

    /**
     * 产品品牌
     * @return \think\model\relation\belongsTo
     */
    public function brand() {
        return $this->belongsTo('Brand', '', '', '', 'LEFT');
    }

    /**
     * 产品分类
     * @return \think\model\relation\belongsTo
     */
    public function category() {
        return $this->belongsTo('Category', '', '', '', 'LEFT');
    }

    /**
     * 产品图片
     * @return \think\model\relation\HasMany
     */
    public function images() {
        return $this->hasMany('ProductImage');
    }

    /**
     * 根据条件获取产品数据
     * @param array $where
     * @return null|static
     */
   public function getProduct($where = []) {
       $where['delete'] = 0;
       return $this->get($where);
   }

    /**
     * 根据条件获取产品列表
     * @param array $where
     * @param int $page
     * @param array $page_param
     * @return \think\Paginator
     */
    public function getList($where = [], $page = 15, $page_param = []) {
        $where['delete'] = 0;
        $data = $this->where($where)->order('sort desc, id desc')->paginate($page, false, ['query' => $page_param]);
        foreach ($data as $k => $v) {
            $thumb = model('ProductImage')->where(['product_id' => $v->id, 'is_main' => 1])->value('thumb');
            $data[$k]['thumb'] = $thumb;
        }
        return $data;
    }

    /**
     * 产品评价
     * @return \think\model\relation\hasMany
     */
    public function productComment() {
        return $this->hasMany('ProductComment', '', '', 'LEFT');
    }
}
